package com.ms.p2p.controller.app;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.RetMap;
import com.ms.p2p.entity.UserThreadLocal;
import com.ms.p2p.entity.content.Article;
import com.ms.p2p.entity.content.ArticleCategory;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.*;
import com.ms.p2p.service.RSAService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chensk on 2018/3/14.
 */
@Controller
@RequestMapping({ "/api" })
public class ApiContentController extends  BaseController{

    @Reference
    private YeahpaysProvider yeahpaysProvider;
    @Reference
    private MemberProvider memberProvider;
    @Reference
    private MemberAccountProvider memberAccountProvider;
    @Reference
    private LoanProvider loanProvider;
    @Reference
    private LoanTenderRecordProvider tenderService;
    @Reference
    private LoanCategoryProvider borrowTypeService;
    @Reference
    private SettingProvider settingProvider;
    @Reference
    private SendProvider smsService;
    @Autowired
    private RSAService rsaService;

    @Reference
    private ArticleCategoryProvider categoryProvider;


    @Reference
    private TestProvider testProvider;


    @Autowired
    private ServletContext applicationScope;

    @Reference
    private ArticleProvider articleProvider;
    @Reference
    private ArticleCategoryProvider artileTypeProvider;
    /**
     * 根据种类获取文章
     */
    @RequestMapping({"/article/list/{cate_name}"})
    @ResponseBody
    public HashMap list(@PathVariable String cate_name,
                        @RequestParam(value = "currentPage", defaultValue = "1", required = false)
                       Integer currentPage, Model model) {


        PageBean page = new PageBean();
        page.setPageSize(8);
        page.setCurrentPage(currentPage);
        ArticleCategory category = artileTypeProvider.getByCateName(cate_name);
        List<ArticleCategory> types = artileTypeProvider.getlist();
        Integer type_id = category.getId();
        List<Article> list = articleProvider.findPager(type_id, "1", page);
        page.setTotalCount(articleProvider.findCount(type_id, "1"));
        HashMap retMap = RetMap.Success("获取数据成功");
        retMap.put("page",page);
        retMap.put("list",list);
        return  retMap;
    }

    /**
     * 新闻公告详情
     */
    @RequestMapping({"/article/{id}"})
    @ResponseBody
    public HashMap toNewsDetail(@PathVariable String id, Model model) {
        List<ArticleCategory> types = artileTypeProvider.getlist();
        Article article = articleProvider.get(id);
        HashMap retMap = RetMap.Success("获取数据成功");
        retMap.put("item",article);
        return retMap;
    }

}
